import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import AdminLogin from '../views/AdminLogin.vue'
import AdminDashboard from '../views/AdminDashboard.vue'
import Home from '../views/Home.vue'
// 管理员页面（用户、代拿者、公告、系统设置）

// 管理员路由守卫
const adminAuthGuard = (to, from, next) => {
  const publicAdminPaths = ['/admin/login'];
  if (to.path.startsWith('/admin') && !publicAdminPaths.includes(to.path)) {
    const adminToken = localStorage.getItem('adminToken');
    const adminInfoRaw = localStorage.getItem('adminInfo');
    let isAdmin = false;
    if (adminInfoRaw) {
      try {
        const info = JSON.parse(adminInfoRaw);
        isAdmin = info && info.role === 'admin';
      } catch (e) {
        isAdmin = false;
      }
    }

    if (!adminToken || !isAdmin) {
      return next('/admin/login');
    }
  }
  next();
};

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/messages',
    name: 'MessageCenter',
    component: () => import('../views/MessageCenter.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/identity',
    name: 'IdentityHome',
    component: () => import('../views/IdentityHome.vue')
  },
  {
    path: '/pickup',
    name: 'Pickup',
    component: () => import('../views/Pickup.vue')
  },
  {
    path: '/order/publish',
    name: 'OrderPublish',
    component: () => import('../views/OrderPublish.vue')
  },
  {
    path: '/order/my',
    name: 'OrderMy',
    component: () => import('../views/OrderMy.vue')
  },
  {
    path: '/order/detail/:id',
    name: 'OrderDetail',
    component: () => import('../views/OrderDetail.vue')
  },
  {
    path: '/payment',
    name: 'Payment',
    component: () => import('../views/Payment.vue')
  },
  // 下单者“我的主页”嵌套路由，左侧模块固定
  {
    path: '/order/profile',
    component: () => import('../views/OrderProfileLayout.vue'),
    children: [
      { path: '', name: 'OrderProfileInfo', component: () => import('../views/OrderProfileInfo.vue') },
      { path: 'address', name: 'OrderAddress', component: () => import('../views/OrderAddress.vue') },
      { path: 'security', name: 'OrderSecurity', component: () => import('../views/OrderSecurity.vue') },
      { path: 'notify', name: 'OrderNotify', component: () => import('../views/OrderNotify.vue') },
      { path: 'help', name: 'OrderHelp', component: () => import('../views/OrderHelp.vue') },
      { path: 'help/faq', name: 'OrderFaq', component: () => import('../views/OrderFaq.vue') },
      { path: 'about', name: 'OrderAbout', component: () => import('../views/OrderAbout.vue') }
    ]
  },
  // 兼容旧路径，全部重定向到新嵌套路径
  { path: '/order/address', redirect: '/order/profile/address' },
  { path: '/order/security', redirect: '/order/profile/security' },
  { path: '/order/notify', redirect: '/order/profile/notify' },
  { path: '/order/help', redirect: '/order/profile/help' },
  { path: '/order/about', redirect: '/order/profile/about' },
  // 代拿员路由
  {
    path: '/courier/hall',
    name: 'CourierOrderHall',
    component: () => import('../views/CourierOrderHall.vue')
  },
  {
    path: '/courier/orders',
    name: 'CourierOrderMy',
    component: () => import('../views/CourierOrderMy.vue')
  },
  {
    path: '/courier/order/detail/:id',
    name: 'CourierOrderDetail',
    component: () => import('../views/CourierOrderDetail.vue')
  },
  {
    path: '/courier/order/rate/:id',
    name: 'CourierOrderRate',
    component: () => import('../views/CourierOrderRate.vue')
  },
  {
    path: '/courier/profile',
    component: () => import('../views/CourierProfileLayout.vue'),
    children: [
      { path: '', name: 'CourierProfile', component: () => import('../views/CourierProfileInfo.vue') },
      { path: 'data', name: 'CourierData', component: () => import('../views/CourierData.vue') },
      { path: 'settings', name: 'CourierSettings', component: () => import('../views/CourierSettings.vue') },
      { path: 'wallet', name: 'CourierWallet', component: () => import('../views/CourierWallet.vue') },
      { path: 'security', name: 'CourierSecurity', component: () => import('../views/CourierSecurity.vue') },
      { path: 'notify', name: 'CourierNotify', component: () => import('../views/CourierNotify.vue') },
      { path: 'help', name: 'CourierHelp', component: () => import('../views/CourierHelp.vue') },
      { path: 'help/faq', name: 'CourierFaq', component: () => import('../views/CourierFaq.vue') },
      { path: 'help/guide', name: 'CourierGuide', component: () => import('../views/CourierGuide.vue') }
    ]
  },
  {
    path: '/mine',
    name: 'Mine',
    component: () => import('../views/Mine.vue')
  },
  {
    path: '/create-order',
    name: 'CreateOrder',
    component: () => import('../views/CreateOrder.vue')
  },
  {
    path: '/order-manage',
    name: 'OrderManage',
    component: () => import('../views/OrderManage.vue')
  },
  // 管理员路由
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: AdminLogin
  },
  {
    path: '/admin/dashboard',
    name: 'AdminDashboard',
    component: AdminDashboard
  },
  {
    path: '/admin/orders',
    name: 'AdminOrders',
    component: () => import('../views/AdminOrders.vue')
  },
  {
    path: '/admin/finance',
    name: 'AdminFinance',
    component: () => import('../views/AdminFinance.vue')
  },
  {
    path: '/admin/logs',
    name: 'AdminLogs',
    component: () => import('../views/AdminLogs.vue')
  },
  {
    path: '/admin/permissions',
    name: 'AdminPermissions',
    component: () => import('../views/AdminPermissions.vue')
  },
  {
    path: '/admin/pricing',
    name: 'AdminPricing',
    component: () => import('../views/AdminPricing.vue')
  },
  {
    path: '/admin/users',
    name: 'AdminUsers',
    component: () => import('../views/AdminUsers.vue')
  },
  {
    path: '/admin/couriers',
    name: 'AdminCouriers',
    component: () => import('../views/AdminCouriers.vue')
  },
  {
    path: '/admin/announcements',
    name: 'AdminAnnouncements',
    component: () => import('../views/AdminAnnouncements.vue')
  },
  {
    path: '/admin/settings',
    name: 'AdminSettings',
    component: () => import('../views/AdminSettings.vue')
  }
  /* {
    path: '/test',
    name: 'Test',
    component: () => import('../views/Test.vue')
  } */
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 应用管理员路由守卫
router.beforeEach(adminAuthGuard);

export default router